package com.group.project.hrms.controller;

import com.group.project.hrms.entity.HolidayList;
import com.group.project.hrms.entity.HolidayRecord;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.IHolidayListService;
import com.group.project.hrms.service.IHolidayRecordService;
import com.group.project.hrms.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class HolidayRecordController {

    @Autowired
    private IHolidayRecordService iHolidayRecordService;

    @Autowired
    private IHolidayListService iHolidayListService;

    @Autowired
    private LogUtil logUtil;

    /**
     * 该方法接收来自前端的请求，查询所有假日记录和假期类型列表并返回给前端
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/getHolidayRecordAndList")
    @ResponseBody
    public ResultEntity getHolidayRecordAndList(HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Map<String, List> map = new HashMap<>();
        List<HolidayRecord> holidayRecords = iHolidayRecordService.selectAllHolidayRecord();
        List<HolidayList> holidayLists = iHolidayListService.selectAllHolidayList();
        map.put("holidayLists", holidayLists);
        map.put("holidayRecords", holidayRecords);
        List<Map> list = new ArrayList<>();
        list.add(map);
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        resultEntity.setDataList(list);
        return resultEntity;
    }

    /**
     * 该方法接收前端传来的创建节假日记录的数据集合，向后台数据库做持久化操作
     * @param map 前端数据集合
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/insertHolidayRecord")
    @ResponseBody
    public ResultEntity insertHolidayRecord(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        if(null != map){
            Object objHolidayDate = map.get("holidayDate");
            Object objHolidayNameId = map.get("holidayNameId");
            Object objHolidayType = map.get("holidayType");
            if(null != objHolidayDate && null != objHolidayNameId && null != objHolidayType){
                try{
                    int year = Integer.parseInt(objHolidayDate.toString().substring(0, 4));
                    int month = Integer.parseInt(objHolidayDate.toString().substring(5, 7));
                    int date = Integer.parseInt(objHolidayDate.toString().substring(8, 10));
                    Calendar calendar = Calendar.getInstance();
                    Long holidayNameId = Long.valueOf(objHolidayNameId.toString());
                    int holidayType = Integer.valueOf(objHolidayType.toString());
                    //检查用户是否已经使用了这个日期，一个日期只能使用在一个节日里
                    List<HolidayRecord> holidayRecords = iHolidayRecordService.selectHolidayRecordByYear(year);
                    if(null != holidayRecords && holidayRecords.size() != 0){
                        for (HolidayRecord hr : holidayRecords
                        ) {
                            calendar.setTime(hr.getAppointDate());
                            if(calendar.get(Calendar.YEAR) == year && calendar.get(Calendar.MONTH) + 1 == month && calendar.get(Calendar.DATE) == date){
                                resultEntity.setCode("F");
                                resultEntity.setMsg("日期已经存在");
                                return resultEntity;
                            }
                        }
                    }
                    //创建HolidayList对象，根据ID查询节日列表
                    HolidayList holidayList = new HolidayList();
                    holidayList.setId(holidayNameId);
                    //开始插入数据
                    SimpleDateFormat sd = new SimpleDateFormat();
                    sd.applyPattern("yyyy-MM-dd");
                    calendar.setTime(sd.parse(year + "-" + month + "-" + date));
                    HolidayRecord holidayRecord = new HolidayRecord();
                    holidayRecord.setcTime(new Date());
                    holidayRecord.setAppointDate(calendar.getTime());
                    holidayRecord.setHolidayId(holidayNameId);
                    holidayRecord.setHolidayName(iHolidayListService.selectOneHolidayList(holidayList).getHolidayName());
                    holidayRecord.setHolidayType(holidayType);
                    int i = iHolidayRecordService.insertHolidayRecord(holidayRecord);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), new Date(), "创建了一天节假日记录", "考勤");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("创建成功");
                        return resultEntity;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }

    /**
     * 该方法接收前端删除请求，向数据库删除指定日期的假期记录
     * @param map 前端传入的要删除的数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/deleteHolidayRecord")
    @ResponseBody
    public ResultEntity deleteHolidayRecord(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        if(map != null){
            Object objId = map.get("id");
            if(objId != null){
                try{
                    Long id = Long.valueOf(objId.toString());
                    HolidayRecord holidayRecord = new HolidayRecord();
                    holidayRecord.setId(id);
                    int i = iHolidayRecordService.deleteHolidayRecord(holidayRecord);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), new Date(), "删除了一条指定日期的假期记录", "考勤");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("删除成功");
                        return resultEntity;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }
}
